<?php
/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @subpackage Query
 * @version 0.1-pre
 */

/**
 * Class is used for a (set of) search term(s) that all have similar properties
 * with regards to things like fuzziness and being optional. 
 * 
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @subpackage Query
 */
class Solphr_Query_Term implements Solphr_Query_Interface {
	private $terms;
	private $field;
	
	/**
	 * Whether the term(s) are required or not.  
	 * Set to true to mandate all word(s) in the query be found for a result.
	 * @var bool
	 */
	public $required = false;
	
	/**
	 * Whether to "negate" a word (foo becomes NOT foo).
	 * @var bool
	 */
	public $negative = false;
	
	/**
	 * Whether to fuzzy search or not.
	 * @var bool
	 */
	public $fuzzy = false; 
	
	/**
	 * The operator used for multiple words. 
	 * TODO: validate OR/AND
	 * @var string
	 */
	public $op = 'OR';
	
	/**
	 * Class constructor.
	 * @param string $term The term(s) to build the instance around.
	 * @param string $field The field in the index to search.  Defaults to Solr's default.
	 */
	public function __construct($term, $field = null) {
		if(is_array($term)) {
			$this->terms = $term;
		} else {
			$this->terms = preg_split("/[\\s,]*\\\"([^\\\"]+)\\\"[\\s,]*|[\\s,]*'([^']+)'[\\s,]*|[\\s,]+/", $term, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);	
		}	
		
		if(isset($field)) {
			$this->field = (string)$field;
		}
	}
	
	/**
	 * @see Solphr/Query/Solphr_Query_Interface#__toString()
	 */
	public function __toString() {
		return $this->buildQuery();
	}
	
	/**
	 * 
	 * @return string the built query.
	 */
	private function buildQuery() {
		if($this->negative === true) {
			$query = 'NOT ';
		} elseif($this->required === true) {
			$query .= '+';
		} else {
			$query = '';
		}
		
		$query .= ($this->field !== false) ? "{$this->field}:" : '';
				
		$query .= (count($this->terms) > 1) ? '(' : '';

		$this->terms = call_user_func(array(Solphr_Request::IMPL, 'queryEscape'), $this->terms);
		 
		foreach($this->terms as &$t) {
			$t .= ($this->fuzzy === true) ? $t .= '~' : '';
		}
		
		$query .= implode(" {$this->op} ", $this->terms);
		
		$query .= (count($this->terms) > 1) ? ')' : '';
		return $query;
	}
}